package com.cj.core.crypto;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;

import java.util.Date;

/**
 * 雪花算法生成唯一ID工具类（支持动态配置机器码）
 */
public class IdUtils extends IdUtil {

    private static final long START_TIMESTAMP = 1732982400000L; // 起始时间戳 (2024-12-01)
    private static final long MACHINE_BIT = 1;   // 机器ID位数
    private static final long DATACENTER_BIT = 1; // 数据中心ID位数

    /**
     * 获取字符串类型的唯一ID
     */
    public static String getIdAsString() {
        Snowflake snowflake = new Snowflake(
                new Date(START_TIMESTAMP),
                MACHINE_BIT,
                DATACENTER_BIT,
                false
        );
        return snowflake.nextIdStr();
    }

    /**
     * 获取长整型的唯一ID
     */
    public static long getIdAsLong() {
        Snowflake snowflake = new Snowflake(
                new Date(START_TIMESTAMP),
                MACHINE_BIT,
                DATACENTER_BIT,
                false
        );
        return snowflake.nextId();
    }

    public static void main(String[] args) {
        System.out.println("字符串："+ getIdAsString());
        System.out.println("数字类："+ getIdAsLong());
        System.out.println("uuid："+ IdUtils.objectId());
    }
}